<?php

include 'vars.php';
include 'functs.php';

Authenticate();

include 'config.mysql.php';

$link = MySQL_go($mysql_server,$mysql_user,$mysql_pass);
MySQL_set_db($link,$mysql_db);

Page_startup($title,$version);
Page_headbar($_SESSION[user],$usertimezone,$systemtimezone);

if (strlen($_GET[search]) > 2) {
	if (!empty($_GET[search])) {

	$search = mysql_real_escape_string($_GET[search]);
	$limit = mysql_real_escape_string($_GET[limit]);

	$searchsql = "SELECT * FROM `featherchat_messages` WHERE `message` LIKE '%".$search."%' ORDER BY `msgid` ASC";
		$searchquery = mysql_query($searchsql) or die ("Search failed.");
		if (!empty($_GET[limit])) {
			$rows = mysql_num_rows($searchquery);
			if ($rows > $_GET[limit]) {
				$searchsql .= " LIMIT ".($rows - $limit).",".$rows;
			}
			$searchquery = mysql_query($searchsql) or die ("Search failed here.");
		}

		$i=0;
		?><table cellspacing="0"><?php

		while ($row = mysql_fetch_assoc($searchquery)) {
			$i++;
			Message_print($row,$_SESSION[user],$i,$i);
		}
		if ($i == 0) { echo "<b>No posts containing '".htmlspecialchars(stripslashes($_GET[search]))."'</b><br />"; }
		?></table><?php
	}
}
elseif (isset($_GET[search])) { echo "Search must be at least 3 characters long."; }
	
?><br /><form method="get" action="search.php">
Search: <input type="text" size="30" name="search" value="<?php echo stripslashes($_GET[search]); ?>" /><br />
Max results: <input type="text" size="2" name="limit" value="<?php if(!empty($_GET[limit])) { echo $_GET[limit]; } else { echo "10"; } ?>"/>
<br /><input type="submit" value="Go" /> | <?php Home_link(); ?>
</form><?php
	
Page_shutdown();

MySQL_halt($link);